<template>
  <div class="login-page">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="cloud cloud-1"></div>
      <div class="cloud cloud-2"></div>
      <div class="cloud cloud-3"></div>
      <div class="mountain mountain-1"></div>
      <div class="mountain mountain-2"></div>
      <div class="mountain mountain-3"></div>
    </div>

    <!-- 悬浮粒子效果 -->
    <div class="floating-particles">
      <div class="particle" v-for="i in 15" :key="i"></div>
    </div>

    <!-- 神秘符文 -->
    <div class="mystical-runes">
      <div class="rune" v-for="(rune, index) in mysticalRunes.slice(0, 15)" :key="index" :data-rune="rune">
        {{ rune }}
      </div>
    </div>

    <!-- 背景光晕 -->
    <div class="background-glow">
      <div class="glow-orb glow-1"></div>
      <div class="glow-orb glow-2"></div>
      <div class="glow-orb glow-3"></div>
      <div class="glow-orb glow-4"></div>
    </div>

    <!-- 能量波纹 -->
    <div class="energy-waves">
      <div class="wave wave-1"></div>
      <div class="wave wave-2"></div>
      <div class="wave wave-3"></div>
    </div>

    <!-- 旋转星环 -->
    <div class="star-rings">
      <div class="star-ring ring-1">
        <div class="star" v-for="i in 12" :key="i"></div>
      </div>
      <div class="star-ring ring-2">
        <div class="star" v-for="i in 8" :key="i"></div>
      </div>
    </div>

    <div class="login-container">
      <div class="login-card ancient-border">
        <!-- 顶部装饰 -->
        <div class="card-decoration">
          <div class="dragon-pattern left"></div>
          <div class="seal-stamp"></div>
          <div class="dragon-pattern right"></div>
        </div>

        <div class="login-header">
          <h1 class="login-title">
            <span class="title-main">生死簿管理系统</span>
            <div class="title-underline"></div>
          </h1>
          <p class="login-subtitle">
            <span class="subtitle-icon">⚡</span>
            仙官身份验证
            <span class="subtitle-icon">⚡</span>
          </p>
        </div>

        <a-form :model="loginForm" :rules="rules" @finish="handleLogin" class="login-form" autocomplete="off">
          <a-form-item name="username">
            <a-input v-model:value="loginForm.username" size="large" placeholder="请输入仙官账号 (默认: admin)" class="mystical-input" autocomplete="off" readonly onfocus="this.removeAttribute('readonly')">
              <template #prefix>
                <UserOutlined class="input-icon" />
              </template>
            </a-input>
          </a-form-item>

          <a-form-item name="password">
            <a-input-password v-model:value="loginForm.password" size="large" placeholder="请输入仙官密码 (默认: admin123)" class="mystical-input" autocomplete="new-password" readonly onfocus="this.removeAttribute('readonly')">
              <template #prefix>
                <LockOutlined class="input-icon" />
              </template>
            </a-input-password>
          </a-form-item>

          <a-form-item>
            <a-button type="primary" html-type="submit" size="large" :loading="loading" class="login-button mystical-button" block>
              <span class="button-text">登录地府系统</span>
              <div class="button-glow"></div>
            </a-button>
          </a-form-item>
        </a-form>

        <!-- 底部装饰 -->
        <div class="bottom-decoration">
          <div class="ancient-pattern"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import { useUserStore } from '@/stores'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(false)
const loginForm = reactive({
  username: '',
  password: ''
})

// 神秘符文数据
const mysticalRunes = ref(['卍', '☯', '☬', '☪', '⚹', '⚡', '☄', '✦', '✧', '✩', '✪', '✫', '✬', '✭', '✮'])

const rules = {
  username: [{ required: true, message: '请输入仙官账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入仙官密码', trigger: 'blur' }]
}

const handleLogin = async () => {
  loading.value = true
  try {
    const result = await userStore.login(loginForm)
    if (result.success) {
      message.success('登录成功，欢迎回到地府系统')
      router.push('/')
    } else {
      message.error('登录失败：' + result.error)
    }
  } catch (error) {
    message.error('登录失败：' + error.message)
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
@use 'sass:math';
.login-page {
  min-height: 100vh;
  background: radial-gradient(ellipse at center, #1a1a2e 0%, #0d1117 50%, #000000 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: $spacing-lg;
  position: relative;
  overflow: hidden;

  // 背景装饰
  .background-decoration {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;

    .cloud {
      position: absolute;
      background: rgba(255, 255, 255, 0.03);
      border-radius: 50px;
      animation: float 20s infinite ease-in-out;

      &.cloud-1 {
        width: 200px;
        height: 60px;
        top: 10%;
        left: 10%;
        animation-delay: 0s;
      }

      &.cloud-2 {
        width: 150px;
        height: 40px;
        top: 20%;
        right: 15%;
        animation-delay: -7s;
      }

      &.cloud-3 {
        width: 180px;
        height: 50px;
        bottom: 15%;
        left: 20%;
        animation-delay: -14s;
      }
    }

    .mountain {
      position: absolute;
      background: linear-gradient(to top, rgba(72, 46, 209, 0.1) 0%, rgba(72, 46, 209, 0.05) 100%);
      clip-path: polygon(0 100%, 30% 0%, 70% 0%, 100% 100%);

      &.mountain-1 {
        width: 300px;
        height: 200px;
        bottom: 0;
        left: 5%;
      }

      &.mountain-2 {
        width: 250px;
        height: 150px;
        bottom: 0;
        right: 10%;
      }

      &.mountain-3 {
        width: 200px;
        height: 120px;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
      }
    }
  }

  // 悬浮粒子效果
  .floating-particles {
    position: absolute;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 2;

    .particle {
      position: absolute;
      width: 2px;
      height: 2px;
      background: $secondary-color;
      border-radius: 50%;
      animation: particleFloat 15s infinite linear;

      @for $i from 1 through 15 {
        &:nth-child(#{$i}) {
          left: math.random() * 100%;
          top: math.random() * 100%;
          animation-delay: math.random() * 15s;
          animation-duration: (10 + math.random() * 10) * 1s;
        }
      }
    }
  }

  // 神秘符文
  .mystical-runes {
    position: absolute;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;

    .rune {
      position: absolute;
      color: rgba(114, 46, 209, 0.3);
      font-size: 24px;
      font-weight: bold;
      animation: runeGlow 4s infinite ease-in-out;

      @for $i from 1 through 15 {
        &:nth-child(#{$i}) {
          top: math.random() * 90%;
          left: math.random() * 90%;
          animation-delay: math.random() * 4s;
          animation-duration: (3 + math.random() * 3) * 1s;
        }
      }
    }
  }

  // 背景光晕
  .background-glow {
    position: absolute;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;

    .glow-orb {
      position: absolute;
      border-radius: 50%;
      filter: blur(40px);
      animation: glowPulse 6s infinite ease-in-out;

      &.glow-1 {
        width: 200px;
        height: 200px;
        background: radial-gradient(circle, rgba(114, 46, 209, 0.2) 0%, transparent 70%);
        top: 10%;
        left: 10%;
        animation-delay: 0s;
      }

      &.glow-2 {
        width: 150px;
        height: 150px;
        background: radial-gradient(circle, rgba(212, 153, 10, 0.15) 0%, transparent 70%);
        top: 15%;
        right: 15%;
        animation-delay: -2s;
      }

      &.glow-3 {
        width: 180px;
        height: 180px;
        background: radial-gradient(circle, rgba(114, 46, 209, 0.1) 0%, transparent 70%);
        bottom: 20%;
        left: 20%;
        animation-delay: -4s;
      }

      &.glow-4 {
        width: 120px;
        height: 120px;
        background: radial-gradient(circle, rgba(212, 153, 10, 0.2) 0%, transparent 70%);
        bottom: 25%;
        right: 25%;
        animation-delay: -6s;
      }
    }
  }

  // 能量波纹
  .energy-waves {
    position: absolute;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;

    .wave {
      position: absolute;
      border: 1px solid rgba(114, 46, 209, 0.1);
      border-radius: 50%;
      animation: waveExpand 8s infinite ease-out;

      &.wave-1 {
        width: 100px;
        height: 100px;
        top: 30%;
        left: 30%;
        animation-delay: 0s;
      }

      &.wave-2 {
        width: 80px;
        height: 80px;
        top: 60%;
        right: 30%;
        animation-delay: -2.5s;
      }

      &.wave-3 {
        width: 120px;
        height: 120px;
        bottom: 30%;
        left: 40%;
        animation-delay: -5s;
      }
    }
  }

  // 旋转星环
  .star-rings {
    position: absolute;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1;

    .star-ring {
      position: absolute;
      top: 50%;
      left: 50%;
      border-radius: 50%;
      animation: ringRotate 30s infinite linear;

      &.ring-1 {
        width: 400px;
        height: 400px;
        margin: -200px 0 0 -200px;
        animation-duration: 30s;
      }

      &.ring-2 {
        width: 300px;
        height: 300px;
        margin: -150px 0 0 -150px;
        animation-duration: -45s;
        animation-direction: reverse;
      }

      .star {
        position: absolute;
        width: 3px;
        height: 3px;
        background: $secondary-color;
        border-radius: 50%;
        box-shadow: 0 0 4px $secondary-color;
        animation: starTwinkle 2s infinite ease-in-out;

        @for $i from 1 through 12 {
          &:nth-child(#{$i}) {
            top: 50%;
            left: 50%;
            transform: translateX(-50%) translateY(-50%) rotate(#{$i * 30}deg) translateX(200px);
            animation-delay: #{$i * 0.2}s;
          }
        }
      }
    }

    .star-ring.ring-2 .star {
      @for $i from 1 through 8 {
        &:nth-child(#{$i}) {
          transform: translateX(-50%) translateY(-50%) rotate(#{$i * 45}deg) translateX(150px);
          animation-delay: #{$i * 0.3}s;
        }
      }
    }
  }

  .login-container {
    width: 100%;
    max-width: 450px;
    z-index: 10;
    position: relative;

    .login-card {
      background: linear-gradient(135deg, rgba(22, 33, 62, 0.9) 0%, rgba(26, 26, 46, 0.95) 50%, rgba(22, 33, 62, 0.9) 100%);
      backdrop-filter: blur(20px);
      border-radius: $border-radius-lg;
      padding: $spacing-xl;
      box-shadow: 0 0 50px rgba(114, 46, 209, 0.3), 0 20px 40px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.1);
      border: 1px solid rgba(114, 46, 209, 0.3);
      position: relative;

      // 顶部装饰
      .card-decoration {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: $spacing-lg;
        position: relative;

        .dragon-pattern {
          width: 80px;
          height: 20px;
          background: linear-gradient(90deg, transparent 0%, $secondary-color 50%, transparent 100%);
          position: relative;

          &::before,
          &::after {
            content: '';
            position: absolute;
            width: 8px;
            height: 8px;
            background: $secondary-color;
            border-radius: 50%;
            top: 50%;
            transform: translateY(-50%);
          }

          &::before {
            left: 10px;
          }

          &::after {
            right: 10px;
          }

          &.left {
            margin-right: $spacing-md;
          }

          &.right {
            margin-left: $spacing-md;
          }
        }

        .seal-stamp {
          width: 40px;
          height: 40px;
          background: radial-gradient(circle, $secondary-color 0%, rgba(212, 153, 10, 0.3) 100%);
          border-radius: 50%;
          position: relative;
          display: flex;
          align-items: center;
          justify-content: center;
          box-shadow: 0 0 20px rgba(212, 153, 10, 0.5);

          &::before {
            content: '阴';
            color: $dark-bg;
            font-weight: bold;
            font-size: 16px;
          }
        }
      }

      .login-header {
        text-align: center;
        margin-bottom: $spacing-xl;

        .login-title {
          margin-bottom: $spacing-md;
          position: relative;

          .title-main {
            color: $secondary-color;
            font-size: 32px;
            font-weight: bold;
            text-shadow: 0 0 20px rgba(212, 153, 10, 0.5);
            background: linear-gradient(45deg, $secondary-color, #ffd700);
            background-clip: text;
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            position: relative;
            z-index: 1;
          }

          .title-underline {
            width: 60px;
            height: 3px;
            background: linear-gradient(90deg, transparent, $secondary-color, transparent);
            margin: $spacing-sm auto 0;
            position: relative;

            &::before,
            &::after {
              content: '';
              position: absolute;
              width: 6px;
              height: 6px;
              background: $secondary-color;
              border-radius: 50%;
              top: 50%;
              transform: translateY(-50%);
            }

            &::before {
              left: -10px;
            }

            &::after {
              right: -10px;
            }
          }
        }

        .login-subtitle {
          color: $text-secondary;
          font-size: $font-size-lg;
          margin: 0;
          display: flex;
          align-items: center;
          justify-content: center;
          gap: $spacing-sm;

          .subtitle-icon {
            color: $secondary-color;
            font-size: 14px;
            animation: sparkle 2s infinite;
          }
        }
      }

      .login-form {
        .mystical-input {
          background: rgba(22, 33, 62, 0.8) !important;
          border: 1px solid rgba(114, 46, 209, 0.3) !important;
          border-radius: $border-radius-md !important;
          transition: all 0.3s ease;

          &:focus,
          &:hover {
            border-color: $secondary-color !important;
            box-shadow: 0 0 15px rgba(114, 46, 209, 0.3) !important;
          }

          .input-icon {
            color: $secondary-color;
          }

          // 覆盖 Ant Design 的默认样式
          :deep(.ant-input) {
            background: rgba(22, 33, 62, 0.8) !important;
            border: none !important;
            color: $text-primary !important;
            box-shadow: none !important;

            &:focus,
            &:hover {
              background: rgba(22, 33, 62, 0.9) !important;
              border: none !important;
              box-shadow: none !important;
            }

            &[readonly] {
              cursor: pointer !important;
              opacity: 1 !important;
              background: rgba(22, 33, 62, 0.8) !important;
            }

            &::placeholder {
              color: rgba(255, 255, 255, 0.5) !important;
            }
          }

          // 密码输入框样式
          :deep(.ant-input-password) {
            background: rgba(22, 33, 62, 0.8) !important;
            border: none !important;

            .ant-input {
              background: rgba(22, 33, 62, 0.8) !important;
              border: none !important;
              color: $text-primary !important;

              &:focus,
              &:hover {
                background: rgba(22, 33, 62, 0.9) !important;
                border: none !important;
                box-shadow: none !important;
              }

              &[readonly] {
                cursor: pointer !important;
                opacity: 1 !important;
                background: rgba(22, 33, 62, 0.8) !important;
              }
            }
          }

          // 兼容其他可能的输入框样式
          input,
          input[type='text'],
          input[type='password'] {
            background: rgba(22, 33, 62, 0.8) !important;
            border: none !important;
            color: $text-primary !important;
            box-shadow: none !important;

            &:focus,
            &:hover,
            &:active {
              background: rgba(22, 33, 62, 0.9) !important;
              border: none !important;
              box-shadow: none !important;
              outline: none !important;
            }

            &:read-only {
              cursor: pointer !important;
              opacity: 1 !important;
            }

            &::placeholder {
              color: rgba(255, 255, 255, 0.5) !important;
            }

            // 覆盖浏览器自动填充样式
            &:-webkit-autofill,
            &:-webkit-autofill:hover,
            &:-webkit-autofill:focus,
            &:-webkit-autofill:active {
              -webkit-box-shadow: 0 0 0 30px rgba(22, 33, 62, 0.8) inset !important;
              -webkit-text-fill-color: $text-primary !important;
              transition: background-color 5000s ease-in-out 0s !important;
            }
          }

          // 额外的自动填充样式覆盖
          :deep(.ant-input:-webkit-autofill),
          :deep(.ant-input:-webkit-autofill:hover),
          :deep(.ant-input:-webkit-autofill:focus),
          :deep(.ant-input:-webkit-autofill:active) {
            -webkit-box-shadow: 0 0 0 30px rgba(22, 33, 62, 0.8) inset !important;
            -webkit-text-fill-color: $text-primary !important;
            transition: background-color 5000s ease-in-out 0s !important;
          }

          // 密码输入框的自动填充样式覆盖
          :deep(.ant-input-password .ant-input:-webkit-autofill),
          :deep(.ant-input-password .ant-input:-webkit-autofill:hover),
          :deep(.ant-input-password .ant-input:-webkit-autofill:focus),
          :deep(.ant-input-password .ant-input:-webkit-autofill:active) {
            -webkit-box-shadow: 0 0 0 30px rgba(22, 33, 62, 0.8) inset !important;
            -webkit-text-fill-color: $text-primary !important;
            transition: background-color 5000s ease-in-out 0s !important;
          }
        }

        .mystical-button {
          background: linear-gradient(45deg, $primary-color 0%, $secondary-color 100%) !important;
          border: none !important;
          height: 52px !important;
          font-size: $font-size-lg !important;
          border-radius: $border-radius-md !important;
          position: relative;
          overflow: hidden;
          transition: all 0.3s ease;

          &:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(114, 46, 209, 0.4);
          }

          &:active {
            transform: translateY(0);
          }

          .button-text {
            position: relative;
            z-index: 2;
            font-weight: 600;
            text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
          }

          .button-glow {
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: linear-gradient(45deg, transparent 30%, rgba(255, 255, 255, 0.1) 50%, transparent 70%);
            animation: buttonGlow 3s infinite;
            pointer-events: none;
          }
        }
      }

      // 底部装饰
      .bottom-decoration {
        margin-top: $spacing-lg;
        text-align: center;

        .ancient-pattern {
          width: 120px;
          height: 20px;
          background: linear-gradient(90deg, transparent 0%, rgba(114, 46, 209, 0.3) 25%, $primary-color 50%, rgba(114, 46, 209, 0.3) 75%, transparent 100%);
          margin: 0 auto;
          position: relative;

          &::before,
          &::after {
            content: '';
            position: absolute;
            width: 30px;
            height: 1px;
            background: $primary-color;
            top: 50%;
            transform: translateY(-50%);
          }

          &::before {
            left: -40px;
          }

          &::after {
            right: -40px;
          }
        }
      }
    }
  }
}

// 动画效果
@keyframes float {
  0%,
  100% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-20px);
  }
}

@keyframes particleFloat {
  0% {
    transform: translateY(100vh) rotate(0deg);
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    transform: translateY(-100px) rotate(360deg);
    opacity: 0;
  }
}

@keyframes yamaBreath {
  0%,
  100% {
    transform: scale(1);
    opacity: 0.06;
  }
  50% {
    transform: scale(1.02);
    opacity: 0.12;
  }
}

@keyframes yamaEyeGlow {
  0%,
  100% {
    box-shadow: 0 0 5px $secondary-color;
    opacity: 0.8;
  }
  50% {
    box-shadow: 0 0 15px $secondary-color;
    opacity: 1;
  }
}

@keyframes yamaHandMove {
  0%,
  100% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(-5px) rotate(5deg);
  }
}

@keyframes yamaAura {
  0%,
  100% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 0.3;
  }
  50% {
    transform: translate(-50%, -50%) scale(1.1);
    opacity: 0.6;
  }
}

@keyframes crownJewelGlow {
  0%,
  100% {
    box-shadow: 0 0 8px rgba(255, 215, 0, 0.8);
    transform: translate(-50%, -50%) scale(1);
  }
  50% {
    box-shadow: 0 0 20px rgba(255, 215, 0, 1);
    transform: translate(-50%, -50%) scale(1.1);
  }
}

@keyframes yamaTitleGlow {
  0%,
  100% {
    text-shadow: 0 0 10px rgba(212, 153, 10, 0.8);
    opacity: 0.4;
  }
  50% {
    text-shadow: 0 0 20px rgba(212, 153, 10, 1);
    opacity: 0.7;
  }
}

@keyframes runeGlow {
  0%,
  100% {
    opacity: 0.3;
    transform: scale(1);
  }
  50% {
    opacity: 0.8;
    transform: scale(1.2);
    text-shadow: 0 0 15px currentColor;
  }
}

@keyframes glowPulse {
  0%,
  100% {
    opacity: 0.4;
    transform: scale(1);
  }
  50% {
    opacity: 0.8;
    transform: scale(1.2);
  }
}

@keyframes waveExpand {
  0% {
    transform: scale(0);
    opacity: 0.8;
  }
  100% {
    transform: scale(4);
    opacity: 0;
  }
}

@keyframes ringRotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes starTwinkle {
  0%,
  100% {
    opacity: 0.6;
    transform: scale(1);
  }
  50% {
    opacity: 1;
    transform: scale(1.5);
  }
}

@keyframes sparkle {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

@keyframes buttonGlow {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}

// 响应式设计
@media (max-width: 768px) {
  .login-page {
    padding: $spacing-md;

    .login-container {
      max-width: 100%;

      .login-card {
        padding: $spacing-lg;

        .login-header {
          .login-title .title-main {
            font-size: 24px;
          }
        }
      }
    }
  }
}
</style>
